<template>
  <div class="my-flex-column w100 h100">
    <el-card class="my-query-box mt8" shadow="never" :body-style="{ paddingBottom: '0' }">
      <el-form :inline="true" @submit.stop.prevent>
        <el-form-item label="数据块">
          <el-input type="number" clearable style="width: 100px" v-model="state.filter.address" placeholder="数据块"
            @keyup.enter="onQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Search" @click="onQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- <el-card> -->
    <el-table :data="state.stationBlockListData" row-key="stationCode" style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <div m="4">
            <h3>数据块信息：</h3>
            <el-table :data="props.row.stationBlock" style="width: 100%" @current-change="onCurrentChange">
              <el-table-column label="所属" prop="blockType" align="center">
                <template #default="{ row }">
                  <el-tag>{{ convertDataBlockToLabel(row.blockType) }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="用途" prop="purpose" align="center">
                <template #default="{ row }">
                  <el-tag>{{ convertPurposeToLabel(row.purpose) }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="地址" prop="address" align="center" />
              <el-table-column label="长度" prop="dataSize" align="center" />
              <el-table-column label="操作" width="80" fixed="right" align="center">
                <template #default="{ row }">
                  <my-dropdown-more icon-only
                    v-auths="['api:admin:permission:assign', 'api:admin:role:update', 'api:admin:role:delete']">
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item @click="onEdit(row)">编辑</el-dropdown-item>
                        <el-dropdown-item @click="onDelete(row)">删除</el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </my-dropdown-more>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="工站" align="center">
        <template #default="{ row }">
          <el-tag type="success">{{ row.stationCode }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产线" align="center">
        <template #default="{ row }">
          <el-tag type="info">{{ row.lineName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80" fixed="right" align="center">
        <template #default="{ row }">
          <el-button v-auth="'api:admin:role:add'" icon="ele-Plus" size="small" text type="primary"
            @click="onAdd(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- </el-card> -->
  </div>
  <station-block-form ref="plcBlockFormRef" :title="state.plcBlockFormTitle"></station-block-form>
</template>
<script setup lang="ts" name="industry/station-block-table-menu">
import { ref, reactive, onMounted, getCurrentInstance, onBeforeMount, defineAsyncComponent } from 'vue'
import { StationBlockGetListOutput, PLCBlockGetListOutput, DBPurposeEnum, DataBlockEnum } from '/@/api/industry/data-contracts'
import { PlcBlockApi } from '/@/api/industry/PlcBlock'
const StationBlockForm = defineAsyncComponent(() => import('./station-block-form.vue'))
const MyDropdownMore = defineAsyncComponent(() => import('/@/components/my-dropdown-more/index.vue'))

import eventBus from '/@/utils/mitt'

const plcBlockFormRef = ref()
const { proxy } = getCurrentInstance() as any
const state = reactive({
  loading: false,
  sureLoading: false,
  plcBlockFormTitle: '',
  lastKey: 0,
  filter: {
    address: 0
  },
  stationBlockListData: [] as StationBlockGetListOutput[],
})


//数据块用途
const DBPurpose = {
  1: '工艺采集',
  2: '告警监控',
  3: '设备配方',
} as const;

//数据块类型
const DataBlock = {
  1: 'PLC',
  2: 'MES'
} as const


//转换
const convertPurposeToLabel = (purpose: DBPurposeEnum) => DBPurpose[purpose] as string;

//转换
const convertDataBlockToLabel = (purpose: DataBlockEnum) => DataBlock[purpose] as string;


onMounted(() => {
  onQuery()
  eventBus.off('refreshPLCBlock')
  eventBus.on('refreshPLCBlock', async () => {
    onQuery()
  })
})

onBeforeMount(() => {
  eventBus.off('refreshRole')
})

const emits = defineEmits<{
  (e: 'table-click', node: PLCBlockGetListOutput | null): void
}>()

const onCurrentChange = (currentRow: PLCBlockGetListOutput) => {
  if (!currentRow) {
    return;
  }
  if (state.lastKey === currentRow.id) {
    state.lastKey = 0
    emits('table-click', null)
  } else {
    state.lastKey = currentRow.id as number
    emits('table-click', currentRow)
  }
}

//查询
const onQuery = async () => {
  state.loading = true
  const res = await new PlcBlockApi().getList(state.filter).catch(() => {
    state.loading = false
  })
  if (res && res.data && res.data.length > 0) {
    state.stationBlockListData = res.data
  } else {
    state.stationBlockListData = []
  }
  state.loading = false
}


//新增
const onAdd = (row: StationBlockGetListOutput) => {
  state.plcBlockFormTitle = `新增【${row.stationCode}】数据块信息`
  plcBlockFormRef.value.open({ stationId: row?.stationId })
}

//修改
const onEdit = (row: PLCBlockGetListOutput) => {
  state.plcBlockFormTitle = `编辑数据块【${row.address}】信息`
  plcBlockFormRef.value.open(row)
}

//删除
const onDelete = (row: PLCBlockGetListOutput) => {
  proxy.$modal
    .confirmDelete(`确定要删除数据块为【${row.address}】?`)
    .then(async () => {
      await new PlcBlockApi().softDelete({ id: row.id }, { loading: true, showSuccessMessage: true })
      onQuery()
    })
    .catch(() => { })
}
</script>